Imports System.Data.SqlClient
Imports System.Data
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoAlmacenDetalleDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarDocumentoAlmacenDetalle(ByVal dad As DocumentoAlmacenDetalle) As Boolean
        Dim con As SqlConnection = conex.obtenerConexion()
        Dim valor As Boolean = False

        Try
            con.Open()

            Dim query As String = "USP_RegistrarDocumentoAlmacenDetalle"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dad.CIA)
                .Parameters.AddWithValue("@SUC", dad.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dad.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dad.DCA_Codigo)
                .Parameters.AddWithValue("@DAD", dad.DAD_Codigo)
                .Parameters.AddWithValue("@ART", dad.ART_Codigo)
                .Parameters.AddWithValue("@CANT", dad.DAD_Cantidad)
                .Parameters.AddWithValue("@ACT", dad.DAD_Activo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function actualizarDocumentoAlmacenDetalle(ByVal dad As DocumentoAlmacenDetalle) As Boolean
        Dim con As SqlConnection = conex.obtenerConexion()
        Dim valor As Boolean = False

        Try
            con.Open()

            Dim query As String = "USP_ActualizarDocumentoAlmacenDetalle"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", dad.CIA)
                .Parameters.AddWithValue("@SUC", dad.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", dad.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dad.DCA_Codigo)
                .Parameters.AddWithValue("@DAD", dad.DAD_Codigo)
                .Parameters.AddWithValue("@ART", dad.ART_Codigo)
                .Parameters.AddWithValue("@CANT", dad.DAD_Cantidad)
                .Parameters.AddWithValue("@ACT", dad.DAD_Activo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerListadoDocumentoAlmacenDetalle(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String) As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenDetalle " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

    Function obtenerDocumentoAlmacenDetalleXID(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String, ByVal codDAD As String) As DocumentoAlmacenDetalle
        Dim dad As New DocumentoAlmacenDetalle("-1", "", "", "", "", "", "", "")
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacenDetalle " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA AND DAD_Codigo = @DAD"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DAD", codDAD)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                dad = New DocumentoAlmacenDetalle(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, _
                                                  ds.Tables(0).Rows(0)("DAD_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ART_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DAD_Cantidad").ToString.Trim, ds.Tables(0).Rows(0)("DAD_Activo").ToString.Trim)
            End If
        Catch ex As Exception
            dad = Nothing
        Finally
            con.Close()
        End Try

        Return dad
    End Function

End Class
